package com.crm.controller;

import com.crm.dto.DeptDto;
import com.crm.entity.Dept;
import com.crm.result.Result;
import com.crm.result.ScrollQuery;
import com.crm.service.IDeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@Slf4j
@RestController
@RequestMapping("/system")
public class DeptController {
    @Autowired
    private IDeptService deptService;
    /**
     * 部门管理,新增
     */
    @PostMapping("/dept")
    public Result save(@RequestBody Dept dept){
        if (dept.getParentId() == null){
            dept.setParentId(0L);
        }
        deptService.savedept(dept);
        return Result.success();
    }
    /**
     * 部门列表，滑动翻页
     */
    @GetMapping("/dept/list")
    public Result<List<Dept>> getDeptPage(ScrollQuery query){
        List<Dept> list = deptService.loadDeptByScroll(query.getDeptName(), query.getStatus());
        return Result.success(list);
    }
    /**
     * 除了上级部门的查询回显
     */
    @GetMapping("/dept/{deptId}")
    public Result<Dept> getDept(@PathVariable("deptId") Integer deptId){
        Dept dept = deptService.getById(deptId);
        return Result.success(dept);
    }
    /**
     * 显示所有部门
     */
    @GetMapping("/dept/list/exclude/{deptId}")
    public Result<List<Dept>> getDeptExclude(@PathVariable("deptId") Integer deptId){
        List<Dept> list = deptService.getlistById(deptId);
        return Result.success(list);
    }
    /**
     * 修改部门
     */
    @PutMapping("/dept")
    public Result updateDept(@RequestBody Dept dept){
        deptService.updateExcludeById(dept);
        return Result.success();
    }
    /**
     * 删除部门
     */
    @DeleteMapping("/dept/{deptId}")
    public Result deleteDept(@PathVariable("deptId") Integer deptId){
        deptService.removeById(deptId);
        return Result.success();
    }
    /**
     * 禁用部门
     */
    @PutMapping("/dept/disable")
    public Result disableDept(@RequestBody DeptDto deptDto){
        deptService.disableDept(deptDto);
        return Result.success();
    }

}
